package top.datacluster.basic.common;

import java.util.List;

public class BinaryTreeNode<T> {

    private BinaryTreeNode leftChild;

    private BinaryTreeNode rightChild;

    private T data;

    public BinaryTreeNode(BinaryTreeNode leftChild, BinaryTreeNode rightChild, T data) {
        this.leftChild = leftChild;
        this.rightChild = rightChild;
        this.data = data;
    }

    /**
     * 采用递归的方式对二叉树先序遍历，存储于List之中
     * @param treeNode 根节点
     * @param result
     */
    public void preTraversal(BinaryTreeNode treeNode, List<BinaryTreeNode> result){
        if (treeNode != null){
            result.add(treeNode);
        }else {
            return;
        }
        preTraversal(treeNode.getLeftChild(), result);
        preTraversal(treeNode.getRightChild(), result);
    }

    /**
     * 采用递归的方式对二叉树中序遍历，存储于List之中
     * @param treeNode 根节点
     * @param result
     */
    public void mediateTraversal(BinaryTreeNode treeNode, List<BinaryTreeNode> result){
        if (treeNode != null){
            mediateTraversal(treeNode.getLeftChild(), result);
        }else{
            return;
        }
        result.add(treeNode);
        mediateTraversal(treeNode.getRightChild(), result);
    }

    /**
     * 采用递归的方式对二叉树后序遍历，存储于List之中
     * @param treeNode
     * @param result
     */
    public void nextTraversal(BinaryTreeNode treeNode, List<BinaryTreeNode> result){
        if (treeNode == null)
            return;
        nextTraversal(treeNode.getLeftChild(), result);
        nextTraversal(treeNode.getRightChild(), result);
        result.add(treeNode);
    }

    public BinaryTreeNode getLeftChild() {
        return leftChild;
    }

    public void setLeftChild(BinaryTreeNode leftChild) {
        this.leftChild = leftChild;
    }

    public BinaryTreeNode getRightChild() {
        return rightChild;
    }

    public void setRightChild(BinaryTreeNode rightChild) {
        this.rightChild = rightChild;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
